/* tripl/main.c 
 * 
 * This file is part of tripl. 
 * 
 * tripl is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * tripl is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with tripl. If not, see <https://www.gnu.org/licenses/>
 */ 




#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#include <sys/mman.h>
#include <sys/types.h>

#include <tripl/tripl.h>



int 	usage(void);
char	*basename, *fbuffer, *fname;


int main(int argc, const char **argv) {
	off_t 	fsize;
	int 	fd;
	int     retstat;

	basename = (char *) argv[0];

	if (argc != 2) 
		return usage();


	if (!(fname = (char *) argv[1])) 
		return usage();

	if ((fd = open(fname, O_RDONLY)) < 0) {
		perror(basename);

		return usage();
	}

	fsize = lseek(fd, 0, SEEK_END);
	lseek(fd, 0, SEEK_SET);


	if ((fbuffer = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
		perror(basename);

		return usage();
	}


	retstat = do_tripl_compile(fbuffer, fsize);

	munmap(fbuffer, fsize);
	close(fd);

	if (retstat) {
		error("compliation failed");

		return retstat;
	}

	return 0;
}


int usage(void) {
	error("it is very easy to learn how to use tripl :-)");
	error("Usage: tripl FILE");

	return 1;
}


